<?php
namespace Addons\Wechat\Controller;
class IndexController extends \Addon
{

    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        $this->type = $type = I("type", "1");
        $this->memuNav = "index_" . $this->type;
        $sql = "store_id='$this->admin_store_id' and type='$type'";
        header("Content-type: text/html; charset=utf-8");
        // dump( strpos($this->k, "搜"));
        /*******回复期数*****/
        if ($this->k = I("k", "", "urldecode")) {
            $k = intval($this->k);
            if ($k > 0) {
                /******回复数字****/
                $k = number2Chinese($k);
                $k = "第{$k}期";
                $sql .= " and (  rule_head = '$k' or rule_head = '$this->k')";
            } else if (checkstr($this->k, "搜")) {
                /******回复带搜****/
                $k = str_replace("搜", "", $this->k);
                /*****页数**/
                $p = findNum($k);
                if (intval($p) > 0) {
                    $_GET['p'] = $p;
                    $k = str_replace($p, "", $k);
                }
                $sql .= "  and (rule_keyword like '%$k%' or rule_reply like '%$k%')";
            }
        }
        $order = "addtime desc";
        $data = BoxModel("addon_wx_search")->get($sql, 10, $order);
        $this->data = $data['list'];
        $this->show_page($data['pager']);
        $this->admin_display();
    }
    /*
    public function test(){
           $model=BoxModel("addon_wx_search");
           $model2=BoxModel("addon_wx_title");
           foreach($data as $item){
               $rule_reply=strip_tags($item['rule_reply'],"<a>");
               $items=$this->change_item($rule_reply);
               $row=array(
                       'rule_head'=>trim(trim($item['rule_head'],"【"),"】"),
                      'rule_reply'=>$items['content'],
                   );
              //dump($items);exit();
              $model->where(array('id'=>$id))->save($row);
               $id=$item['id'];
               $model2->where("topic_id='$id'")->delete();
              foreach($items['items'] as $one1){
                  $p2=array(
                     'topic_id'=>$id,
                     'content'=>$one1,
                     'store_id'=>$item['store_id'],
                    );
                  if($item['type']==1){
                      $model2->add($p2);
                  }
              }
           }
       }*/
    /******发布商品****/
    public function add()
    {
        $model = BoxModel("addon_wx_search");
        $id = I("id");
        $this->data = $model->load($id);
        $this->type = I('type', 1);
        $this->memuNav = "index_2";
        $this->admin_display();
    }

    public function change_item($content)
    {
        $content = str_replace("\n", "", $content);
        $pat = '/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/i';
        preg_match_all($pat, $content, $m);
        $href = array();
        foreach ($m[4] as $k => $one) {
            $a = trim($m[2][$k]);
            $text = trim($one);
            if ($text) {
                $href[] = '<a href="' . $a . '">' . $text . '</a>';
            } else {
                $href[] = "";
            }
        }
        foreach ($m[0] as $k => $one) {
            $str = $href[$k] ? "<br />" . $href[$k] : "";
            $content = str_replace($one, $str, $content);
        }
        return array(
            'content' => $content,
            'items' => $href,
        );
    }


    public function actadd()
    {
        $id = I('id');
        $store_info = session('store_info');
        $this->admin_store_id = $store_info['store_id'];
        $model = BoxModel("addon_wx_search");
        $rule_reply = strip_tags(stripslashes(htmlspecialchars_decode($_POST['rule_reply'])), "<a>");
        $this->type = I('type', 1);
        $items = $this->change_item($rule_reply);
        $p = array(
            'rule_head' => trim(trim(I('rule_head'), "【"), "】"),
            'rule_reply' => $items['content'],
            'rule_keyword' => I('rule_keyword'),
            'store_id' => $this->admin_store_id,
            'type' => $this->type,
            'addtime' => date("Y-m-d H:i:s"),
        );
        if ($id) {
            unset($p['addtime']);
            $model->where("id='$id'")->save($p);
            $url = "/BoxAddon/Wechat/Index/add/type/{$this->type}/id/$id";
        } else {
            $id = $model->add($p);
            $url = "/BoxAddon/Wechat/Index/index/type/{$this->type}";
        }
        /******保存标题***/
        $model2 = BoxModel("addon_wx_title");
        $model2->where("topic_id='$id'")->delete();
        foreach ($items['items'] as $one1) {
            $p2 = array(
                'topic_id' => $id,
                'content' => $one1,
                'store_id' => $this->admin_store_id,
            );
            if ($this->type == 1) {
                $model2->add($p2);
            }
        }
        $this->jump("操作成功", $url);
    }


    /*************删除操作*****************/
    public function del()
    {
        $id = I("id");
        BoxModel("addon_wx_search")->where("id='$id'")->delete();
        BoxModel("addon_wx_title")->where("topic_id='$id'")->delete();
        $this->jump("删除成功");
    }


}